package main

import (
	"encoding/csv"
    "context"
    "fmt"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "log"
    "time"
	"os"
)

func main() {
    parseArgs()

    client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
    if err != nil {
        log.Fatalf("创建 MongoDB 客户端失败: %v", err)
    }

    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    err = client.Connect(ctx)
    if err != nil {
        log.Fatalf("连接 MongoDB 失败: %v", err)
    }
    defer client.Disconnect(ctx)

    fmt.Println("MongoDB 连接成功")
	var writer *csv.Writer
	var file *os.File
	if !saveResults {
		fmt.Println(">>> 注意: 未启用结果保存，查询结果不会保存到 CSV 文件中。")
		writer = (*csv.Writer)(nil)
	} else {
		file, writer = createCSVFile()
		defer file.Close()
	}

    // 开始磁盘监控 & 阈值触发查询
    monitorAndTrigger(client, timeWindows, writer)
}
